<template>
  <div class="page-container">
    <!-- 面包屑部分 -->
    <div class="breadcrumb pt15">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>会员中心</el-breadcrumb-item>
        <el-breadcrumb-item>VIP会员管理</el-breadcrumb-item>
        <el-breadcrumb-item>VIP设置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!--工具栏-->
    <div class="toolbar" >
      <el-form :inline="true" :model="filters" :size="size">
        <el-row class="search-input">
        <el-form-item   style="margin-bottom:0 !important">
          <el-form-item label="创建时间："  style="margin-right:0 !important">
            <el-date-picker
              v-model="filters.startTime"
              type="datetime"
              placeholder="创建时间始"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="changeStartDate"
              :picker-options="startDateDisabled"
              clearable
            ></el-date-picker>
          </el-form-item>
            <span class="line">-</span>
            <el-form-item prop="endTime">
              <el-date-picker
                v-model="filters.endTime"
                type="datetime"
                placeholder="创建时间末"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                @change="changeEndDate"
                :picker-options="endDateDisabled"
                clearable
              ></el-date-picker>
          </el-form-item>
        </el-form-item>
          <el-form-item label="VIP名称：">
            <el-input v-model.trim="filters.vipName" clearable placeholder="VIP名称"></el-input>
          </el-form-item>
        </el-row>
        <el-row class="search-button button20">
            <el-form-item>
              <kt-button
                icon="el-icon-search"
                :label="'搜索'"
                type="primary"
                @click="findPage(null)"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                :plain="true"
                icon="fa fa-refresh" :label="$t('action.reset')"
                type="primary"
                @click="resetForm('filters')"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                :label="$t('action.add')"
                type="primary"
                @click="handleAdd"
                v-if="operateDis"
              />
            </el-form-item>
        </el-row>
      </el-form>
    </div>

    <Jic-table
      :height="350"
      :tableList="pageResult"
      :tableHeaderList="filterColumns"
      @findPage="findPage"
      :showBatchDelete="false"
      :showEditOperation="true"
      :showDeleteOperation="true"
      @handleDelete="handleDelete"
      :totalSize="totalSize"
      @handleEdit="handleEdit"
      @handlePageChange="handlePageChange"
      @handleSizeChange="handleSizeChange"
      :showSelection="false"
      :sortable="false"
      ref="JicTable"
      :showIndex="false"
    ></Jic-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      class="el-dialog-vip"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      @closed="handleClose"
      width="40%"
    >
      <el-form
        :model="dataForm"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-row class="searchDialog-input">
           <el-form-item label="VIP名称：" prop="vipName"  class="longInput">
          <el-input
            v-model.trim="dataForm.vipName"
            maxlength="20"
            placeholder="最多输入20个字符"
            auto-complete="off"
            clearable

          ></el-input>
        </el-form-item>
        <el-form-item label="VIP价格：" prop="vipPrice" class="shortInput">
          <el-input
            v-model.trim="dataForm.vipPrice"
            auto-complete="off"
            oninput="value=value.replace(/[^\d]/g,'')"
            maxlength="5"
            min="0"
            class="mr10 "
            clearable
            placeholder="请输入VIP价格"
          ></el-input>
          <!-- 元/年 -->
        </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false" plain type="primary">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import JicTable from "@/components/jicComponentGallery/jicTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import { exportExcel } from "@/utils/excel";
export default {
  components: {
    PopupTreeInput,
    JicTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      startDateDisabled: {},//开始时间限制
      endDateDisabled: {},//结束时间限制
      deptType: 0,
      operateDis: true,
      totalSize: 0,
      size: "small",
      filters: {
        vipName: null,
        endTime: null,
        startTime: null
      },
      columns: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: [],
      options: {},
      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        vipName: [
          { required: true, message: "请输入等级名称", trigger: "blur" }
        ],
        vipPrice: [
          { required: true, message: "请输入VIP价格", trigger: "blur" }
        ]
      },
      // 新增编辑界面数据
      dataForm: {
        vipName: null,
        vipPrice: null
      }
    };
  },
  methods: {
    changeStartDate: function() {
      var this_ = this;
      this_.endDateDisabled = {
        disabledDate: time => {
          if (this_.filters.startTime) {
            return time.getTime() <= new Date(this_.filters.startTime).getTime();
          }
        }
      };
    },
    changeEndDate: function() {
      var this_ = this;
      this_.startDateDisabled = {
        disabledDate: time => {
          if (this_.filters.endTime) {
            return time.getTime() >= new Date(this_.filters.endTime).getTime();
          }
        }
      };
    },
    handleClose() {
      this.$refs.dataForm.resetFields();
    },
    // 获取分页数据
    findPage: function(data) {
      let this_ = this;
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      this_.utils.request.queryMemberVipList(this_.filters, function(res) {
        this_.pageResult = res.data.rows;
        this_.totalSize = Number(res.data.total);
      });
    },

    //左右分页点击
    handlePageChange(params) {
      if (params != null && params.number != null) {
        if (params.number != null) {
          this.pageRequest.pageNum = params.number;
        }
        this.findPage();
      }
    },
    // 换页刷新
    handleSizeChange: function(pageSize) {
      if (pageSize != null) {
        this.pageRequest.pageSize = pageSize.size;
        this.findPage();
      }
    },

    // 删除
    handleDelete: function(data) {
      this.$confirm("确认删除吗？", "提示", {}).then(() => {
        var params = {};
        params.id = data.row.id;

        var this_ = this;
        this.utils.request.delMemberVip(params, function(res) {
          if (res.data == "1") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.findPage(null);
          } else {
            this_.$message({
              message: "操作失败, " + res.msg,
              type: "error"
            });
          }
        });
      });
    },
    // 显示新增界面
    handleAdd: function() {
      this.dialogVisible = true;
      this.operation = true;
      this.dataForm = {};
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          let this_ = this;
          let params = Object.assign({}, this_.dataForm);
          params.token = localStorage.getItem("token");
          if (this_.dataForm.id != null) {
            this.utils.request.editMemberVip(params, function(res) {
              if (res.data == "1") {
                this_.$message({ message: "操作成功", type: "success" });

                this_.$refs["dataForm"].resetFields();

                this_.dialogVisible = false;

                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          } else {
            this.utils.request.addMemberVipInfo(params, function(res) {
              if (res.data == "1") {
                this_.$message({ message: "操作成功", type: "success" });

                this_.$refs["dataForm"].resetFields();

                this_.dialogVisible = false;

                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          }
        }
      });
    },

    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "vipName", label: "VIP名称", minWidth: 50 },
        { prop: "deptName", label: "所属组织" },
        {
          prop: "deptType",
          label: "所属层级",
          minWidth: 120,
          formatter: this.typeFormatter
        },
        { prop: "createTime", label: "创建时间", minWidth: 120 }
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    resetForm(formName) {
      this.filters = {
        vipName: null,
        endTime: null,
        startTime: null
      };
      this.findPage();
    },
    //商家类型转化
    typeFormatter: function(row, column, cellValue, index) {
      if (cellValue == 0) {
        return "母平台";
      } else if (cellValue == 1) {
        return "子平台";
      } else if (cellValue == 2) {
        return "商家";
      } else if (cellValue == 3) {
        return "店铺";
      }
    },
  },

  mounted() {
    this.initColumns();
    this.findPage();
  }
};
</script>

<style scoped lang='scss'>
// .breadcrumb {
//   margin-bottom: 50px !important;
// }
/deep/ .el-form-item__error {
  left: 35%;
  transform: translateX(-50%);
}
.searchDialog-input{
  /deep/ .el-form-item {
    margin-bottom: 30px !important;

    // 搜索输入框标签
    .el-form-item__label {
      text-align: right;
    }
    // 搜索输入框
    .el-form-item__content {
      .el-input__inner {
        // width: 178px;
        height: 36px;
        line-height: 36px;
        display: inline-block;
      }
      .el-date-editor.el-input,
      .el-date-editor.el-input__inner {
        width: auto;
      }
    }
  }
}
</style>
